<?php
header('content-type:imagepng');
/**
 * Created by PhpStorm
 * User:caidongliang
 * date:
 */

session_start();
//1.创建画布
$img = imagecreatetruecolor(200,50);
//2.设置颜色
$bgcolor = imagecolorallocate($img,255,255,255);
//3.填充颜色
imagefill($img,0,0,$bgcolor);
//4.显示随机文字 ,显示几个文字就循环几次
  $code = '';
  for ($i = 0;$i < 4;$i ++) {
  	$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
  	$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    $x = 200/4*$i + 10;
    $y = (50+40)/2;
    $text = $str[mt_rand(0,strlen($str)-1)];

  	//对象，文字大小，旋转角度
  	imagettftext($img, 30, mt_rand(-40,40), $x, $y, $color, 'simsun.ttc', $text);
  	$code.= $text;
  }
  $_SESSION['code'] = $code;
  //加干扰 dian..
   for ($i = 0;$i < 1000;$i ++) {
   	 $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
   	 imagesetpixel($img,mt_rand(0,200), mt_rand(0,100), $color);
   }
//加线
  for ($i = 0;$i < 15;$i ++) {
   	 $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
   	 imageline($img,mt_rand(0,200), mt_rand(0,100),mt_rand(0,200),mt_rand(0,100), $color);
   }

//输出画布
imagepng($img);
//释放图片
imagedestroy($img);